<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 单项管理
 */
namespace app\home\model;

use app\home\model\Model;

class Single extends Model{

    private $single = 'web_single';
    private $contractcate = 'sys_contractcate';
    private $article = 'sys_article';
    private $articlecate = 'sys_articleclass';

    /**
     * 单项信息
     * @param array $single
     * @param array $where
     */
    public function single($where='', $lang='default',$file='*')
    {
        $singleinfo = Model::name($this->single)
            ->field($file)
            ->where($where)
            ->find();
        if(!empty($singleinfo['introduction'])){
            $singleinfo['introduction']=isset(json_decode($singleinfo['introduction'],true)[$lang])?json_decode($singleinfo['introduction'],true)[$lang]:'';  
        }
        if(!empty($singleinfo['titles'])){
            $singleinfo['title']=isset(json_decode($singleinfo['titles'],true)[$lang])?json_decode($singleinfo['titles'],true)[$lang]:$singleinfo['title'];
        }
        return $singleinfo;    
    }

    /**
     * 单项文件栏目信息
     * @param array $concate
     * @param array $where
     */
    public function concate($where='', $lang='default',$file='*')
    {
        $singleinfo = Model::name($this->contractcate)
            ->field($file)
            ->where($where)
            ->limit(0,4)
            ->order('id asc')
            ->select()
            ->toArray();
        foreach ($singleinfo as $key => $value) {
            if(!empty($singleinfo[$key]['introduction'])){
                $singleinfo[$key]['introduction']=isset(json_decode($value['introduction'],true)[$lang])?json_decode($value['introduction'],true)[$lang]:'';  
            }
            if(!empty($singleinfo[$key]['titles'])){
                $singleinfo[$key]['title']=isset(json_decode($value['titles'],true)[$lang])?json_decode($value['titles'],true)[$lang]:$value['title'];
            }
            if(!empty($singleinfo[$key]['content'])){
                $singleinfo[$key]['content']=isset(json_decode($value['content'],true)[$lang])?htmlspecialchars_decode(json_decode($value['content'],true)[$lang]):'';
            }
            unset($singleinfo[$key]['titles']);
        }
        return $singleinfo;    
    }

    /**
     * 类别信息
     * @param array $singlecate
     * @param array $where
     */
    public function artcate($where='', $lang='default',$file='*')
    {
        $singlecate = Model::name($this->articlecate)
            ->field($file)
            ->where($where)
            ->limit(0,4)
            ->order('orderid asc, id asc')
            ->select()
            ->toArray();
        foreach ($singlecate as $key => $value) {
            if(!empty($value['introduction'])){
                $singlecate[$key]['introduction']=isset(json_decode($value['introduction'],true)[$lang])?json_decode($value['introduction'],true)[$lang]:'';  
            }
            if(!empty($value['titles'])){
                $singlecate[$key]['title']=isset(json_decode($value['titles'],true)[$lang])?json_decode($value['titles'],true)[$lang]:$value['title'];
            }
            unset($singlecate[$key]['titles']);
        }
        return $singlecate;    
    }

    /**
     * 类别信息
     * @param array $articlelist
     * @param array $where
     */
    public function articlelist($where='', $lang='default',$file='*')
    {
        $singlelist = Model::name($this->article)
            ->field($file)
            ->where($where)
            ->limit(0,4)
            ->order('id desc')
            ->select()
            ->toArray();
        if(!empty($singlelist)){
            foreach ($singlelist as $key => $value) {
                if(!empty($value['introduction'])){
                    $singlelist[$key]['introduction']=isset(json_decode($value['introduction'],true)[$lang])?json_decode($value['introduction'],true)[$lang]:'';  
                }
                if(!empty($value['titles'])){
                    $singlelist[$key]['title']=isset(json_decode($value['titles'],true)[$lang])?json_decode($value['titles'],true)[$lang]:$value['title'];
                }
                unset($singlelist[$key]['titles']);
            }
        }
        
        return $singlelist;    
    }

    /**
     * 类别信息
     * @param array $singlecate
     * @param array $where
     */
    public function singlecate($where='',$lang='default',$file='*')
    {
        $singlecate = Model::name($this->single)
            ->field($file)
            ->where($where)
            ->order('orderid asc')
            ->select()
            ->toArray();
        foreach ($singlecate as $key => $value) {
            if(strpos($value['imgurl'], ',')!== false){
                $singlecate[$key]['imgurl']=explode(',', $value['imgurl'])[1];
                $singlecate[$key]['mimgurl']=explode(',', $value['imgurl'])[0];
            }
            if(!empty($value['introduction'])){
                $singlecate[$key]['introduction']=isset(json_decode($value['introduction'],true)[$lang])?json_decode($value['introduction'],true)[$lang]:'';  
            }
            if(!empty($value['titles'])){
                $singlecate[$key]['title']=isset(json_decode($value['titles'],true)[$lang])?json_decode($value['titles'],true)[$lang]:$value['title'];
            }
            unset($singlecate[$key]['titles']);
        }
        return $singlecate;    
    }

}
?>